home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / CL_SplitLog23b.lha / splitlog2.3 / source / splitlog23.c
C/C++ Source or Header  |  1996-09-30  |  13KB  |  485 lines

  1. /*
  2. **  $VER: SplitLog.c 2.3 (22 Aug 1996)
  3. **
  4. **        © 1996 Ralf Ramge, Nicolai Zizka
  5. **
  6. **  PROGRAMNAME:
  7. **      SplitLog
  8. **
  9. **  FUNCTION:
  10. **      split CL5.0 Logfiles
  11. */
  12.  
  13. #include <exec/memory.h>
  14. #include <mui/demo.h>
  15. #include <cl/types.h>
  16. #include <cl/cll.h>
  17. #include <proto/locale.h>
  18. #include <libraries/locale.h>
  19. #include <utility/tagitem.h>
  20.  
  21. #define LogfilePath "CONNECTLINE:Log"
  22. #define SublogfilePath "CONNECTLINE:Log/Sublogs"
  23. #define LogfileName "CONNECTLINE:Log/Logfile"
  24. #define BakLogFile "Logfile-bak."
  25.  
  26. #define MSG_MMSG_PROJECT 0
  27. #define MSG_MMSG_PROJECT_STR "Project"
  28. #define MSG_PROGRESS 1
  29. #define MSG_PROGRESS_STR "Progress Display"
  30. #define MSG_TOTAL 2
  31. #define MSG_TOTAL_STR "Completed:"
  32. #define MSG_WAITING 3
  33. #define MSG_WAITING_STR "<Waiting>"
  34. #define MSG_ENTRIES 4
  35. #define MSG_ENTRIES_STR " Entries"
  36. #define MSG_NOMEM 5
  37. #define MSG_NOMEM_STR "SplitLog terminated - insufficient memory!"
  38. #define MSG_NOOPEN 6
  39. #define MSG_NOOPEN_STR "SplitLog terminated - failed to open '%s'!"
  40. #define MSG_NOCREATE 7
  41. #define MSG_NOCREATE_STR "SplitLog terminated - could not create '%s'!"
  42. #define MSG_NOLOGLIB 8
  43. #define MSG_NOLOGLIB_STR "Error: Could not open cllog.library.\n"
  44. #define MSG_FAILED 9
  45. #define MSG_FAILED_STR "SplitLog terminated - DOS-Error :"
  46.  
  47. #define MMSG_ABOUT      "Info..."
  48. #define aboutstring     "\33b\33cCL-SplitLog 2.3\33n\n© 1996 Ralf Ramge, Nicolai Zizka\n\n\33iMUI\33n © Stefan Stuntz"
  49.  
  50. /* Variablen */
  51.  
  52. ULONG filelaenge;
  53. APTR MyPool;
  54. char nfilename[50]="";
  55.  
  56. /* Strukturen */
  57.  
  58. typedef struct zeilen zeilen;
  59.  
  60. struct zeilen {
  61.     char *zeile;
  62.     zeilen *next;
  63.     };
  64.  
  65. typedef struct logid logid;
  66.  
  67. struct logid {
  68.     char *name;
  69.     ULONG anzahl;
  70.     zeilen *inhalt;
  71.     logid *next;
  72.     };
  73.  
  74. enum
  75. {
  76.     MEN_PROJECT, MEN_ABOUT
  77.     };
  78.  
  79. static struct NewMenu MUIMenuData[]=
  80. {
  81.     {   NM_TITLE,   MSG_MMSG_PROJECT_STR,   0 , 0 , 0 , (APTR)MEN_PROJECT   },
  82.     {   NM_ITEM,    MMSG_ABOUT          ,  "?", 0 , 0 , (APTR)MEN_ABOUT     },
  83.     };
  84.  
  85. struct AppString
  86. {
  87.     LONG   as_ID;
  88.     STRPTR as_Str;
  89. };
  90.  
  91. struct AppString AppStrings[] =
  92. {
  93.     {MSG_MMSG_PROJECT,MSG_MMSG_PROJECT_STR},
  94.     {MSG_PROGRESS,MSG_PROGRESS_STR},
  95.     {MSG_TOTAL,MSG_TOTAL_STR},
  96.     {MSG_WAITING,MSG_WAITING_STR},
  97.     {MSG_ENTRIES,MSG_ENTRIES_STR},
  98.     {MSG_NOMEM,MSG_NOMEM_STR},
  99.     {MSG_NOOPEN,MSG_NOOPEN_STR},
  100.     {MSG_NOCREATE,MSG_NOCREATE_STR},
  101.     {MSG_NOLOGLIB,MSG_NOLOGLIB_STR},
  102.     {MSG_FAILED,MSG_FAILED_STR},
  103. };
  104.  
  105.  
  106. /* Zeiger */
  107.  
  108. APTR app,window,aboutwin,bytemeter,GA_Total,GA_Sublog,menustrip;
  109. struct Library *CLLBase;
  110.  
  111.  
  112. /* Prototypen */
  113.  
  114. int createapp(void);
  115. int gethighestsuffix(void);
  116. logid *readlog (void);
  117. logid *addlogidptr(char *name, char *zeile);
  118. zeilen *addlineptr(char *zeile);
  119. void failed2open(char *failedfile);
  120. void refreshwin(void);
  121. void ReadStrings(void);
  122. void startbatch(void);
  123. BOOL renamelog (void);
  124. BOOL makedir(char *CreateNew);
  125. BOOL savelogs(logid *pointer);
  126.  
  127.  
  128. /* MUI-Fenster-Definition */
  129.  
  130. int createapp()
  131. {
  132.     app =   ApplicationObject,
  133.             MUIA_Application_Title      , "CL-SplitLog",
  134.             MUIA_Application_Version    , "$VER: CL-SplitLog 2.3 (22.08.96)",
  135.             MUIA_Application_Copyright  , "©1996 Ralf Ramge/Nicolai Zizka",
  136.             MUIA_Application_Author     , "Ralf Ramge, Nicolai Zizka",
  137.             MUIA_Application_Description, "Splitten des Logfiles von Connectline 5.0",
  138.             MUIA_Application_Base       , "SPLITLOG",
  139.             MUIA_Application_Menustrip  , menustrip = MUI_MakeObject (MUIO_MenustripNM, MUIMenuData,0),
  140.  
  141.             SubWindow, window = WindowObject,
  142.  
  143.                 MUIA_Window_Title   , "CL-SplitLog",
  144.                 MUIA_Window_ID      , MAKE_ID('S','P','L','T'),
  145.                 MUIA_Window_CloseGadget, FALSE,
  146.  
  147.                 WindowContents, VGroup,
  148.  
  149.                     Child, bytemeter = TextObject, TextFrame, MUIA_Text_PreParse, "\33c", MUIA_Text_Contents, "0", End,
  150.  
  151.                     Child, ColGroup(2), GroupFrameT(AppStrings[MSG_PROGRESS].as_Str),
  152.                         Child, Label(AppStrings[MSG_TOTAL].as_Str), Child, GA_Total = GaugeObject, GaugeFrame, MUIA_Gauge_Current,0, MUIA_Gauge_InfoText, AppStrings[MSG_WAITING].as_Str, MUIA_Gauge_Horiz, TRUE, End,
  153.                         Child, Label("Sublog:"), Child, GA_Sublog = GaugeObject, GaugeFrame, MUIA_Gauge_Current, 0, MUIA_Gauge_InfoText, AppStrings[MSG_WAITING].as_Str, MUIA_Gauge_Max, 100, MUIA_Gauge_Horiz, TRUE, End,
  154.                         Child, VSpace(0)       , Child, ScaleObject, End,
  155.                         End,
  156.                     End,
  157.                 End,
  158.             End;
  159.  
  160.     if (!app)
  161.     {
  162.         fail(app,"Failed to create application.");
  163.         return 0;
  164.         }
  165.  
  166.     DoMethod(window,MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  167.              app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);  //CloseRequest
  168.     set(window,MUIA_Window_Open,TRUE);
  169.     return 1;
  170.     }
  171.  
  172.  
  173. /* alloziiere neuen logid * */
  174.  
  175. logid *addlogidptr(char *name, char *zeile)
  176. {
  177.     logid *tmplptr;
  178.     if ((tmplptr = AllocPooled(MyPool,sizeof(logid)))!=NULL) {
  179.         if ((tmplptr->name = AllocPooled(MyPool,strlen(name)+1))!=NULL) {
  180.             strcpy(tmplptr->name,name);
  181.             tmplptr->inhalt = addlineptr(zeile);
  182.             tmplptr->next = NULL;
  183.             tmplptr->anzahl = 1;
  184.             }
  185.         else
  186.             tmplptr=NULL;
  187.         }
  188.     return tmplptr;
  189. }
  190.  
  191. /* alloziiere neuen zeilen * */
  192.  
  193. zeilen *addlineptr(char *zeile)
  194. {
  195.     zeilen *tmpzptr;
  196.     if ((tmpzptr = AllocPooled(MyPool,sizeof(zeilen)))!=NULL) {
  197.         if ((tmpzptr->zeile = AllocPooled(MyPool,strlen(zeile)+1))!=NULL) {
  198.             strcpy(tmpzptr->zeile,zeile);
  199.             tmpzptr->next = NULL;
  200.             }
  201.         else
  202.             tmpzptr=NULL;
  203.         }
  204.     return tmpzptr;
  205. }
  206.  
  207. /* Logfile lesen */
  208.  
  209. logid *readlog (void)
  210. {
  211.     ULONG tmplaenge=0,linectr=0;
  212.     char currentid[32] = "",line[1024];
  213.     BPTR file;
  214.     logid *root=NULL,*pointer;
  215.     zeilen *tmpptr;
  216.     struct FileInfoBlock *fib;
  217.     int prozentin;
  218.     char statuszeile[25];
  219.  
  220.     fib=AllocDosObject(DOS_FIB,NULL);
  221.  
  222.     if ((file = Open(LogfileName, MODE_OLDFILE))) {
  223.         ExamineFH(file,fib);
  224.         filelaenge=fib->fib_Size;
  225.  
  226.         while (FGets(file,line,4096)) {
  227.             refreshwin();
  228.  
  229.             pointer = root;
  230.             linectr++;
  231.  
  232.             tmplaenge += strlen(line);
  233.             prozentin = (int) (tmplaenge+1)*100/(filelaenge+1);
  234.             sprintf(statuszeile,"%ld Byte (%d%%), %ld %s",tmplaenge,prozentin,linectr,AppStrings[MSG_ENTRIES].as_Str);
  235.             set(bytemeter, MUIA_Text_Contents, statuszeile);
  236.  
  237.             sscanf(line,"%*s %*s %s %*s",currentid);
  238.  
  239.             if (pointer != NULL) {
  240.                 while ((pointer->next != NULL)&&(stricmp(pointer->name,currentid) != 0))
  241.                     pointer = pointer->next;
  242.  
  243.                 if (stricmp(pointer->name,currentid) == 0) {
  244.                     tmpptr=pointer->inhalt;
  245.  
  246.                     while (tmpptr->next != NULL)
  247.                         tmpptr = tmpptr->next;
  248.  
  249.                     tmpptr->next = addlineptr(line);
  250.                     pointer->anzahl++;
  251.                     }
  252.                 else {
  253.                     pointer->next = addlogidptr(currentid,line);
  254.                     pointer=pointer->next;
  255.                     }
  256.                 }
  257.             else {
  258.                  pointer = addlogidptr(currentid,line);
  259.                  root=pointer;
  260.                  }
  261.  
  262.             if ((pointer==NULL)||(pointer->inhalt==NULL)||(pointer->name==NULL)||(pointer->inhalt->zeile==NULL)) {
  263.                 CLL_AddLogA (5,"SPLITLOG",AppStrings[MSG_NOMEM].as_Str,NULL);
  264.                 root=NULL;
  265.                 break;
  266.                 }
  267.             }
  268.         Close(file);
  269.         }
  270.     else
  271.          failed2open(LogfileName);
  272.  
  273.     FreeDosObject(DOS_FIB,fib);
  274.     return root;
  275. }
  276.  
  277.  
  278. /* einzelne sublogs speichern */
  279.  
  280. BOOL savelogs(logid *pointer)
  281. {
  282.     BPTR file;
  283.     char out[110]="",pro[5];
  284.     ULONG subtotal=0,templaenge=0;
  285.     zeilen *tempptr;
  286.     int prozentout;
  287.  
  288.     if (!makedir(SublogfilePath))
  289.         return FALSE;
  290.  
  291.     set (GA_Total, MUIA_Gauge_Max, filelaenge);
  292.     while(pointer != NULL) {
  293.         sprintf(out,"%s/%s",SublogfilePath,pointer->name);
  294.  
  295.         if (!(file = Open(out,MODE_OLDFILE))) {
  296.             if (!(file = Open(out,MODE_NEWFILE))) {
  297.                 failed2open(out);
  298.                 return FALSE;
  299.                 }
  300.             }
  301.         else
  302.             Seek(file,0,OFFSET_END);
  303.  
  304.         tempptr = pointer->inhalt;
  305.  
  306.         set(GA_Sublog, MUIA_Gauge_Max, pointer->anzahl);
  307.         set(GA_Sublog, MUIA_Gauge_InfoText, pointer->name);
  308.         refreshwin();
  309.  
  310.         while(tempptr != NULL) {
  311.             FPuts(file,tempptr->zeile);
  312.             subtotal++;
  313.             templaenge += strlen(tempptr->zeile);
  314.             tempptr = tempptr->next;
  315.             prozentout = (int) (templaenge+1)*100/(filelaenge+1);
  316.             sprintf(pro,"%d%%",prozentout);
  317.             set(GA_Total, MUIA_Gauge_Current, templaenge);
  318.             set(GA_Total, MUIA_Gauge_InfoText, pro);
  319.             set(GA_Sublog, MUIA_Gauge_Current, subtotal);
  320.             }
  321.  
  322.         Close(file);
  323.  
  324.         pointer = pointer->next;
  325.         subtotal = 0;
  326.         }
  327.     return TRUE;
  328. }
  329.  
  330. /* Batchdatei aufrufen */
  331.  
  332. void startbatch()
  333. {
  334.     SetVar("LOGBACKUPNAME",nfilename,-1,GVF_LOCAL_ONLY);
  335.     Execute(strcat("CONNECTLINE:s/splitlog.batch ",nfilename),NULL,NULL);
  336.     }
  337.  
  338. /* file lies sich nicht öffnen */
  339.  
  340. void failed2open(char *failedfile)
  341. {
  342.     char *failedstr="";
  343.  
  344.     sprintf(failedstr,AppStrings[MSG_NOOPEN].as_Str,failedfile);
  345.     CLL_AddLogA (5,"SPLITLOG",failedstr,NULL);
  346. }
  347.  
  348. /* Directory anlegen */
  349.  
  350. BOOL makedir(char *CreateNew)
  351. {
  352.     BPTR lock;
  353.     char *failedstr="";
  354.  
  355.     if ((lock = Lock(CreateNew,ACCESS_WRITE))==0)
  356.         if ((lock = CreateDir(CreateNew))==NULL)
  357.         {
  358.               sprintf(failedstr,AppStrings[MSG_NOCREATE].as_Str,CreateNew);
  359.               CLL_AddLogA (5,"SPLITLOG",failedstr,NULL);
  360.               return FALSE;
  361.               }
  362.  
  363.     UnLock(lock);
  364.     return TRUE;
  365. }
  366.  
  367. void refreshwin(void)
  368. {
  369.     long id;
  370.     ULONG sigs;
  371.  
  372.     id = DoMethod(app,MUIM_Application_NewInput, &sigs);
  373.     switch (id)
  374.     {
  375.         case MEN_ABOUT:
  376.             MUI_Request(app,aboutwin,0,NULL,"OK",aboutstring);
  377.             break;
  378.         }
  379. }
  380.  
  381. /* Lokalisierung */
  382.  
  383. void ReadStrings()
  384. {
  385.     long ctr;
  386.     APTR catalog;
  387.  
  388.     catalog = OpenCatalog(NULL,"CL-SplitLog.catalog",OC_BuiltInLanguage,"english",TAG_DONE);
  389.  
  390.     for (ctr=0; ctr<10; ctr++)
  391.         AppStrings[ctr].as_Str = GetCatalogStr(catalog,ctr,AppStrings[ctr].as_Str);
  392.  
  393.     CloseCatalog(catalog);
  394.  
  395.     strcpy(MSG_MMSG_PROJECT_STR, AppStrings[MSG_MMSG_PROJECT].as_Str);
  396. }
  397.  
  398. /* aktuellen suffix ermitteln */
  399.  
  400. int gethighestsuffix(void)
  401. {
  402.     BPTR lock;
  403.     struct FileInfoBlock *fib;
  404.     BOOL cont=TRUE;
  405.     char *ppattern="",pattern[15]="Logfile-bak.#?",*buffer="";
  406.     int len=0,highest=0;
  407.  
  408.     fib=AllocDosObject(DOS_FIB,NULL);
  409.     lock=Lock(LogfilePath,ACCESS_READ);
  410.  
  411.     ParsePatternNoCase(pattern,ppattern,2*(strlen(pattern))+2);
  412.     Examine (lock,fib);
  413.  
  414.     while (cont) {
  415.         cont=ExNext (lock,fib);
  416.         if ( MatchPatternNoCase (ppattern,fib->fib_FileName))
  417.             if (highest<atoi(&fib->fib_FileName[12]))
  418.                 highest = atoi (&fib->fib_FileName[12]);
  419.         }
  420.  
  421.     Fault(IoErr(),AppStrings[MSG_FAILED].as_Str,buffer,len);
  422.  
  423.     if (len!=0) {
  424.         CLL_AddLogA (5,"SPLITLOG",buffer,NULL);
  425.         highest=-1;
  426.         }
  427.  
  428.     UnLock(lock);
  429.     FreeDosObject(DOS_FIB,fib);
  430.     return highest;
  431. }
  432.  
  433. /* logfile umbennen */
  434.  
  435. BOOL renamelog (void)
  436. {
  437.     int high;
  438.  
  439.     if ((high=gethighestsuffix())!=-1) {
  440.         sprintf(nfilename,"%s/%s%04d",LogfilePath,BakLogFile,++high);
  441.         CLL_RenameLog (nfilename);
  442.         return TRUE;
  443.         }
  444.     return FALSE;
  445. }
  446.  
  447. /* Hauptprogramm */
  448.  
  449. int main (void)
  450. {
  451.     logid *root;
  452.  
  453.     if (CLLBase=OpenLibrary("connectline:libs/cllog.library",0)) {
  454.  
  455.         ReadStrings();
  456.  
  457.         if ((MyPool = CreatePool(MEMF_ANY,10000,10000))!=NULL) {
  458.             init();
  459.             if (createapp()) {
  460.                 if((root = readlog())!=NULL) {
  461.                      if (renamelog()) {
  462.                          if (savelogs(root)) {
  463.  
  464.                              startbatch();
  465.  
  466.                              refreshwin();
  467.                              }
  468.                          }
  469.                      }
  470.                 set (window,MUIA_Window_Open,FALSE);
  471.                 MUI_DisposeObject(app);
  472.                 fail(NULL,NULL);
  473.                 DeletePool(MyPool);
  474.                 }
  475.             }
  476.         else
  477.             CLL_AddLogA (5,"SPLITLOG",AppStrings[MSG_NOMEM].as_Str,NULL);
  478.  
  479.         CloseLibrary (CLLBase);
  480.         }
  481.     else
  482.         printf("%s\n", AppStrings[MSG_NOLOGLIB].as_Str);
  483.  
  484. }
  485.